using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
namespace BLL
{

    public class BaseBLL
    {
        public CACHE_KEY DependencyKey;
        public CACHE_KEY TableKey;
        public BaseBLL()
        {
            DependencyKey = CACHE_KEY.Null;
            TableKey = CACHE_KEY.Null;
        }
        protected void CacheExpiration()
        {
            if (! (DependencyKey == CACHE_KEY.Null))
            {
                HttpRuntime.Cache.Insert(DependencyKey.ToString(), Guid.NewGuid().ToString());
            }
        }
        private void CheckDependencyCache(string strKey)
        { 
            strKey = strKey.Trim();
            if (strKey == "")
            {
                return;
            }
            if (HttpRuntime.Cache[strKey] == null)
            {
                HttpRuntime.Cache.Insert(strKey, " ", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        private void CheckDependencyCache()
        {
            if (DependencyKey != CACHE_KEY.Null)
            {
                if (HttpRuntime.Cache[this.DependencyKey.ToString()] == null)
                {
                    HttpRuntime.Cache.Insert(this.DependencyKey.ToString(), " ", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
                }
                
            }
            
        }
        protected bool ExistCache(string key)
        {
            if (HttpRuntime.Cache[key] != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        protected void InsertDependendyCache(string key, object obj)
        {
            CheckDependencyCache();
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(null, new string[] { this.DependencyKey.ToString() });
            HttpRuntime.Cache.Insert(key, obj, dep, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
                
        }
        protected void InsertDependendyCache(string key, object obj, string[] arrDependendyKey,DateTime absoluteExpiration,TimeSpan slidingExpiration)
        {
            for (int i = 0; i < arrDependendyKey.Length; i++)
            {
                CheckDependencyCache(arrDependendyKey[i]);
            }
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(null, arrDependendyKey);
            HttpRuntime.Cache.Insert(key, obj, dep, absoluteExpiration, slidingExpiration);
        }
    }
}
